package org.geektimes.configuration.microprofile.better.config.source;

import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @Author: menglinggang
 * @Date: 2021-03-30
 * @Time: 3:29 下午
 */
public class DefaultResourceConfig extends MapBaseConfigSource {

    Logger logger = Logger.getLogger(DefaultResourceConfig.class.getName());

    private static final String configFileLocation = "META-INF/microprofile-config.properties";

    public DefaultResourceConfig() {
        super("DefaultResourceConfig", 100);
    }

    @Override
    protected void prepareConfigData(Map propertiesMap) throws Throwable{
        ClassLoader classLoader = this.getClass().getClassLoader();
        URL resource = classLoader.getResource(configFileLocation);
        if (resource == null) {
            logger.info("The default config file can't be found in the classpath : " + configFileLocation);
            return;
        }
        try (InputStream inputStream = resource.openStream()) {
            Properties properties = new Properties();
            properties.load(inputStream);
            propertiesMap.putAll(properties);
        }
    }
}
